package edu.wuster.blog.controller;

import edu.wuster.blog.entity.Collect;
import edu.wuster.blog.entity.Result;
import edu.wuster.blog.service.CollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    //新增收藏
    @PostMapping
    public Result addCollect(@RequestBody Collect collect) {
        log.info("用户新增收藏");
        Integer id = collectService.addCollect(collect);
        log.info("用户新增收藏成功");
        return Result.success(id);
    }

    //删除收藏
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("用户删除收藏");
        collectService.deleteById(id);
        log.info("用户删除收藏成功");
        return Result.success();
    }

    //查看收藏
    @GetMapping("/{userId}")
    public Result selectByUserId(@PathVariable Integer userId) {
        log.info("用户（id：{}）查看收藏", userId);
        List<Collect> list = collectService.selectByUserId(userId);
        if (list.isEmpty()){
            log.info("收藏为空");
            return Result.success();
        }else {
            log.info("用户查看收藏成功");
            return Result.success(list);
        }
    }
}
